const Controller = require('egg').Controller
const nodemailer = require('nodemailer')

class EmailController extends Controller {
  async mailer() {
    const { ctx } = this
    const { receiver, title, content } = ctx.request.body
    let transporter = nodemailer.createTransport({
      host: 'smtp.qq.com',
      port: 465,
      secure: true, // true for 465
      auth: {
        user: 'mayapony@foxmail.com',
        pass: 'bxqyturxqqmyjche',
      },
    })
    let info = await transporter.sendMail({
      from: '"maya 😍" <mayapony@foxmail.com>',
      to: receiver,
      subject: title,
      html: content,
    })
    ctx.body = {
      body: ctx.request.body,
      info: info,
    }
    ctx.set('Access-Control-Allow-Origin', '*')
  }
}

module.exports = EmailController
